<?php
require_once 'conf/rdbms_viewer.conf.php';
require_once 'lib/rdbms_viewer.class.php';
require_once 'HTML/Template/Sigma.php'; 

$options    =   array('dsns' =>  array('rdbms_viewer' => $conf['rdbms_dsn']));
$rdbms      =   new rdbms_viewer($options);

if(isset($_POST['node'])) {
    parse_str($_POST['node'], $node);
    $dsn_id     =   (isset($node['dsn_id'])) ? $node['dsn_id'] : 1;
} else {
    $node       =   false;
}

switch(true) {
    
    // List tables
    case isset($node['dsn_id']) && isset($node['database']):
    
        $dsn    =   $rdbms->fetch_dsn($dsn_id);
        // connect to database and list tables
        $rdbms->set_database_handle($dsn);
        $tables     =  $rdbms->get_tables(  $dsn['username'].'@'.$dsn['hostspec'], 
                                            $node['database']
                                            );
        $items      =  array();
        
        foreach($tables as $table) {
            $items[]    =   array(  'text'  =>  $table,
                                    'id'    =>  "dsn_id=$dsn_id&database=".$node['database']."&table=$table",
                                    'cls'   =>  'page',
                                    'leaf'  =>  true,
                                    'dsn_id'=>  $dsn_id,
                                    'table' =>  $table,
                                    'database' => $node['database'],
                                    'viewTitle' => $dsn['username'].'@'.$dsn['hostspec'].'/'.$node['database'].' - '.$table
                                    /*
                                    'extended'  =>  array(  'viewTitle' =>  $dsn['username'].'@'.$dsn['hostspec'].'/'.$node['database'].' - '.$table,
                                                            'dsn_id'    =>  $dsn_id,
                                                            'database'  =>  $database,
                                                            'table'     =>  $table
                                                            ) */
                                    );
        }
        
    break;
    
    // fetch databases
    case isset($node['dsn_id']):
       
        // The default node var has a value of "source".  Not sure how to get around this.
        $dsn    =   $rdbms->fetch_dsn($dsn_id);
        
        
        
        // connect to database 
        $rdbms->set_database_handle($dsn);
        $databases  =   $rdbms->get_databases($dsn['username'].'@'.$dsn['hostspec']);
        $items      =   array();
        
        foreach($databases as $item) {
            $items[]    =   array(  'text'  =>  $item,
                                    'id'    =>  "dsn_id=$dsn_id&database=$item",
                                    'cls'   =>  'folder',
                                    'leaf'  =>  false,
                                    'param' =>  'sucka beware'
                                    );
        }
    
    break;

    // Top level - fetch dsn list
    default:
        
        $dsns   =   $rdbms->fetch_dsn();
        $items  =   array();
        
        foreach($dsns as $dsn) {
            $items[]    =   array(  'text'  =>  $dsn['username'].'@'.$dsn['hostspec'],
                                    'id'    =>  'dsn_id='.$dsn['dsn_id'],
                                    'cls'   =>  'folder',
                                    'leaf'  =>  false,
                                    'param' =>  'meow_node'
                                  );
        }

}

error_log(var_export($_POST, true));
echo json_encode($items);

?>
